package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bo.AppBO;

public class GetAppTotalPageAction extends Action{
	private static Logger logger = Logger.getLogger(LoginAction.class);
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String cat = request.getParameter("category");
		String pricefilter = request.getParameter("pricefilter");
		int numberinpage;
		if(request.getParameter("numberinpage")==null){
			numberinpage = 12;
		} else{
			try
			{
			numberinpage = Integer.parseInt(request.getParameter("numberinpage"));
			}
			catch(NumberFormatException e){
				numberinpage = 12;
			}
		}
		if(cat==null){
			cat = "all";
		}
		if(pricefilter==null){
			pricefilter = "all";
		}
		
		AppBO appbo = new AppBO();
	
		try {
			int totalpage = appbo.getNumberOfPageApp(cat, pricefilter,numberinpage);
			PrintWriter out = response.getWriter();
			out.print(totalpage);
			out.close();
			return null;
		} catch (DAOException | IOException e) {
			logger.error(e.getMessage());
			return mapping.findForward("failed");
		}
	}
}
